<?php

class fwDb {

    private $obj;

    function __construct() {
        require_once dirname(__FILE__) . DS . "Generic.php";
        $this->obj = $this->connect();
        $this->obj->setCacher(S::cache());
    }

    /**
     * @return DbSimple_Generic_Database
     */
    private function connect() {
        return DbSimple_Generic::connect(S::config()->getDatabase("dsn"));
    }

    /**
     * mixed query(string $query [, $arg1] [,$arg2] ...)
     * Alias for select(). May be used for INSERT or UPDATE queries.
     * @return mixed 
     */
    function query() {
        return call_user_func_array(array($this->obj, "query"), func_get_args());
    }
    /**
     * mixed select(string $query [, $arg1] [,$arg2] ...)
     * Execute query and return the result.
     * @return mixed
     */
    function select() {
        return call_user_func_array(array($this->obj, "select"), func_get_args());
    }

    /**
     * Returns various statistical information.
     * @return array 
     */
    function getStatistics() {
        return call_user_func_array(array($this->obj, "getStatistics"), func_get_args());
    }

    /**
     * string escape(mixed $s, bool $isIdent=false)
     * Enclose the string into database quotes correctly escaping
     * special characters. If $isIdent is true, value quoted as identifier 
     * (e.g.: `value` in MySQL, "value" in Firebird, [value] in MSSQL).
     * @return string
     */
    function escape() {
        return call_user_func_array(array($this->obj, "escape"), func_get_args());
    }

    /**
     * scalar selectCell(string $query [, $arg1] [,$arg2] ...)
     * Return the first cell of the first column of query result.
     * If no one row selected, return null.
     * @return scalar
     */
    function selectCell() {
        return call_user_func_array(array($this->obj, "selectCell"), func_get_args());
    }

    /**
     * array selectCol(string $query [, $arg1] [,$arg2] ...)
     * Return the first column of query result as array.
     * @return array
     */
    function selectCol() {
        return call_user_func_array(array($this->obj, "selectCol"), func_get_args());
    }

    /**
     * hash selectRow(string $query [, $arg1] [,$arg2] ...)
     * Return the first row of query result.
     * On errors return null and set last error.
     * If no one row found, return array()! It is useful while debugging,
     * because PHP DOES NOT generates notice on $row['abc'] if $row === null
     * or $row === false (but, if $row is empty array, notice is generated).
     * @return hash
     */
    function selectRow() {
        return call_user_func_array(array($this->obj, "selectRow"), func_get_args());
    }

    /**
     * mixed selectPage(int &$total, string $query [, $arg1] [,$arg2] ...)
     * Execute query and return the result.
     * Total number of found rows (independent to LIMIT) is returned in $total
     * (in most cases second query is performed to calculate $total).
     * @return array 
     */
    function selectPage() {
        return call_user_func_array(array($this->obj, "selectPage"), func_get_args());
    }

    /**
     * Создать новую транзакцию
     */
    function transaction() {
        return call_user_func_array(array($this->obj, "transaction"), func_get_args());
    }

    /**
     * Откатить последний шаг транзакции
     */
    function rollback() {
        return call_user_func_array(array($this->obj, "rollback"), func_get_args());
    }

    /**
     * Подтвердить транзакцию
     */
    function commit() {
        return call_user_func_array(array($this->obj, "commit"), func_get_args());
    }
    
    function getTables() {
        $cache=S::cache()->get(md5(APP)."show_tables_result");
        if(!$cache) {
            $cache = $this->selectCol("SHOW TABLES");
            S::cache()->save(md5(APP)."show_tables_result", $cache);
        }
        return $cache;
    }

}